clear all
set mem 9999999
set more off

cd "$dir\Raw_Data"

use base_data.dta, clear

capture drop _merge
merge m:1 school1 using "Schools12.dta"

/*Drop all the students that started school as Freshman and students who are in
Medical or graduate school*/
keep if FR_nonFR_status =="never enrolled as FR" | FR_nonFR_status ==""
drop if MAJR_CODE=="DMED"
drop if Class=="GR"


drop if Class =="UP"
drop if Class=="SP"
drop if Class=="PG"
drop if Class=="UP"

destring Grade, generate (grade) force

bys Term Subject CourseNum: egen mean_coursegrade=mean(grade) 
bys Term Subject CourseNum: egen sd_coursegrade=sd(grade) 
bys Term Subject CourseNum: gen st_coursegrade=(grade-mean_coursegrade)/sd_coursegrade

tostring Term, generate(str_geocode)
gen Year = substr(str_geocode,1,4)
destring Year , generate(year) force

bys ID Term: egen SemGPA = mean(st_coursegrade)
bys ID year: egen YearGPA = mean(st_coursegrade)



*First major
bys ID: stripolate  first_major2 year, gen(firstrealmajor2) groupwise
bys ID: stripolate  first_major year, gen(firstrealmajor) groupwise /*Including Majorless*/

*This is for first year major chosen (i.e. not majorless).
gen major_science=1 if firstrealmajor2=="MATA" | firstrealmajor2=="MATS" | firstrealmajor2=="APPM" | firstrealmajor2=="PHYS" | firstrealmajor2=="CMPS" | firstrealmajor2=="CHEM" | firstrealmajor2=="GEOL" 
replace major_science=1 if firstrealmajor2=="CONS" | firstrealmajor2=="EECE" | firstrealmajor2=="EELE" | firstrealmajor2=="ECIV" | firstrealmajor2=="ECMP" | firstrealmajor2=="EMCH"  | firstrealmajor2=="CHME" | firstrealmajor2=="INDE"
replace major_science=0 if firstrealmajor2!="" & major_science==. 

/*Variale bla created below determines the number of courses a student has been in school
at the observation.*/
sort ID Term
bys ID  : gen bla= _n 
bys ID : egen maxbla = max(bla)
bys ID :  egen minbla = min(bla) if year>=2004
gen newbla = 1 if minbla==bla


gen lastsem = 1 if bla==maxbla 


*generate legacy
encode lgcy_desc, gen (ny2)

gen legacy = 1 if ny2 !=.
replace legacy = 0 if ny2==.
drop ny2

drop if MAJR_CODE=="CHME" |  MAJR_CODE=="CONS" |  MAJR_CODE=="ELEM" | MAJR_CODE=="ELIT" | MAJR_CODE=="ELNG" |  MAJR_CODE=="GEOL" |  MAJR_CODE=="INDE" |  MAJR_CODE=="LDAR" |  MAJR_CODE=="MATA" |  MAJR_CODE=="MAUD" |  MAJR_CODE=="MCOM" |  MAJR_CODE=="MIMG" |  MAJR_CODE=="SOAN" |  MAJR_CODE=="STAT" 
drop if MAJR_CODE=="MJRL" | MAJR_CODE=="MJLS"
*****************************************************************************
*																			*
*				Treatment variables and Controls							*
*																			*
*****************************************************************************
*Create Treatment Variables
*Create year ID for advisor
gen advisorIDyear=fake_advisor_id+year


*Fill in school type for some of the missing years*
bys ID: egen mmuslim=mean(muslim)
bys ID: egen mchristian=mean(christian)

drop if mmuslim==.5 | (mmuslim>1 & mmuslim<98)
drop if mchristian==.5 | (mchristian>1 & mchristian<98)

*Drop students who did their high school outside Lebanon
drop if mmuslim==98 | mchristian==98

gen islam_school=(mmuslim==1)
gen christ_school=(mchristian==1)
gen secular_school=1 if islam_school!= 1 & christ_school!=1
replace secular_school=0 if secular_school==.



*total for all peers
bys fake_advisor_id Term: gen newtotal=_N -1

bys fake_advisor_id Term: egen sum_islam=sum(islam_school)
bys fake_advisor_id Term: gen summus1out=sum_islam-islam_school
bys fake_advisor_id Term: gen prop_islam=summus1out/newtotal

bys fake_advisor_id Term: egen sum_christ=sum(christ_school)
bys fake_advisor_id Term: gen sumchris1out=sum_chris-christ_school
bys fake_advisor_id Term: gen prop_christ=sumchris1out/newtotal

bys fake_advisor_id Term: egen sum_sec=sum(secular_school)
bys fake_advisor_id Term: gen sumsec1out=sum_sec-secular_school
bys fake_advisor_id Term: gen prop_sec=sumsec1out/newtotal

*Generate Interaction Terms accross religious schools
gen christianchrist2=christ_school*prop_christ
gen christianislam2=christ_school*prop_islam
gen christiansec2=christ_school*prop_sec

gen islamicchrist2=islam_school*prop_christ
gen islamicislam2=islam_school*prop_islam
gen islamicsec2=islam_school*prop_sec

gen secularchrist2=secular_school*prop_christ
gen secularislam2=secular_school*prop_islam
gen secularsec2=secular_school*prop_sec


*Standardize treatment variables*
egen stmusl=std(prop_islam)
egen stcath=std(prop_christ)
egen stsec=std(prop_sec)
 
gen christianchrist3=christ_school*stcath
gen  islamicchrist3=islam_school*stcath
 
gen christianislam3=christ_school*stmusl
gen islamicislam3=islam_school*stmusl
 
 *Gender of student
  tab StudGender, gen(sex)
 *Gender of advisor
  tab ADV_GENDER, gen(advsex)
 *Rank of advisor
  replace Rank="NA" if Rank=="" 
  tab Rank, gen(advrank)
  
  *fill in missing SAT*
	bys ID: egen satmat = mean(math_sat)
	bys ID: egen satverb = mean(verbal_sat)
	
*Create quality of peers variable
bys Term fake_advisor_id: egen qualchris=mean(satmat) if christ_school ==1
bys Term fake_advisor_id: egen qualitychris=mean(qualchris)
gen christ_qualchris = qualitychris*christ_school
gen musl_qualchris = qualitychris*islam_school

bys Term fake_advisor_id: egen qualmusl=mean(satmat) if islam_school==1
bys Term fake_advisor_id: egen qualitymusl=mean(qualmusl)
gen christ_qualmusl = qualitymusl*christ_school
gen musl_qualmusl = qualitymusl*islam_school


bys Term fake_advisor_id: egen qualsec=mean(satmat) if secular_school==1
bys Term fake_advisor_id: egen qualitysec=mean(qualsec)
gen christ_qualitysec = qualitysec*christ_school
gen musl_qualitysec = qualitysec*islam_school

*Create gender of peers variable
gen sex = 1 if sex1==1
replace sex= 0 if sex2==1
bys Term fake_advisor_id: egen meanpeersex = mean(sex)
gen christmeanpeersex = christ_school* meanpeersex 
gen muslmeanpeersex = islam_school*meanpeersex


bys  Term CourseNum Subject  instructor_sex: gen TotalClass = _N

gen religion = 1 if islam_school==1
replace religion = 2 if christ_school==1
replace religion = 3 if secular_school==1

bys fake_advisor_id Term CourseNum Subject religion  instructor_sex: gen peersreligion = _N


gen yozz = peersreligion if islam_school ==1
bys fake_advisor_id Term CourseNum Subject  instructor_sex:  egen yoz = mean(yozz)
gen peersmuslim = yoz  - 1 if islam_school==1
replace peersmuslim = yoz if islam_school!=1
gen outcomemusl = peersmuslim/(TotalClass-1)
replace outcomemusl=0 if outcomemusl==.
drop yozz 
drop yoz 


gen yozz = peersreligion if christ_school ==1
bys fake_advisor_id Term CourseNum Subject  instructor_sex:  egen yoz = mean(yozz)
gen peerschrist = yoz  - 1 if christ_school==1
replace peerschrist = yoz if christ_school!=1
gen outcomechrist = peerschrist/(TotalClass-1)
replace outcomechrist=0 if outcomechrist==.
drop yozz 
drop yoz

gen yozz = peersreligion if secular_school ==1
bys fake_advisor_id Term CourseNum Subject  instructor_sex:  egen yoz = mean(yozz)
gen peerssecular = yoz  - 1 if secular_school==1
replace peerssecular= yoz if secular_school!=1
gen outcomesecular = peerssecular/(TotalClass-1)
replace outcomesecular=0 if outcomesecular==.
drop yozz 
drop yoz


 cd "$dir\Final_Data"
drop _merge
merge m:1 ID using Final_sample.dta, keepusing(islam_school christ_school stmusl prop_islam prop_christ stcath christianchrist3 christianislam3   islamicchrist3 islamicislam3)


 gen newtreatm = prop_islam if bla==1
bys ID: egen newtreatmusl = mean(stmusl)


gen christstmusl2 = christ_school*newtreatmusl
gen islamstmusl2 = islam_school*newtreatmusl


 gen newtreatc = prop_christ if bla==1
bys ID: egen newtreatchrist  = mean(stcath )
egen stcath2=std(newtreatchrist)

gen christstcath2 = christ_school*newtreatchrist
gen islamstcath2 = islam_school*newtreatchrist


 gen newtreats = prop_sec if bla==1
bys ID: egen newtreatsec  = mean(newtreats )
egen stcsec2=std(newtreatsec)

*finally controlling for department level proportions
bys Dept Term: gen newtotal2=_N-1
bys Dept Term: egen sum_islam2=sum(islam_school)
bys Dept Term: gen summus1out2=sum_islam2-islam_school
bys Dept Term: gen prop_islam2=summus1out2/newtotal2

bys Dept Term: egen sum_christ2=sum(christ_school)
bys Dept Term: gen sumchris1out2=sum_christ2-christ_school
bys Dept Term: gen prop_christ2=sumchris1out2/newtotal2

bys Dept Term: egen sum_sec2=sum(secular_school)
bys Dept Term: gen sumsec1out2=sum_sec2-secular_school
bys Dept Term: gen prop_sec2=sumsec1out2/newtotal2




 													***********************************************************************************************************
 													**************************Table 2-- First Year course taking*************************
													***********************************************************************************************************
cd "$dir\Tables"										
label variable newtreatmusl "Proportion Islamic school peers"
label variable newtreatchrist "Proportion Christian school peers"

qui{
areg outcomemusl islam_school christ_school newtreatmusl newtreatsec prop_islam2 prop_christ2 prop_sec2  i.year satmat satverb legacy sex* advsex* advrank*  if year>=2002 & year<=2017 & bla<=10 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & SemGPA!=. & MAJR_CODE!="MJRL" & qualitychris!=. & qualitymusl!=. ,  absorb( Dept) vce(cluster advisorIDyear)
	gen sampletag=1 if e(sample)	
}
													
 areg outcomemusl islam_school christ_school newtreatmusl newtreatchrist prop_islam2 prop_christ2 prop_sec2  i.year satmat satverb legacy sex* advsex* advrank*  if sampletag==1 ,  absorb( Dept) vce(cluster advisorIDyear)
eststo maj1 



areg  outcomechrist islam_school christ_school newtreatmusl  newtreatchrist prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if sampletag==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj2

areg  outcomesecular islam_school christ_school newtreatmusl  newtreatchrist  prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if sampletag==1 ,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj3

esttab maj* using Table2.tex, label  nocon keep(newtreatmusl  newtreatchrist) b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) replace

														*************************************************************************************************
														***************Table B1--First Year course taking behavior by religion*****************************
														*************************************************************************************************
**Hetoregenous course outcomes*
*For students from Islamic Schools:
areg outcomemusl islam_school christ_school newtreatmusl newtreatchrist prop_islam2 prop_christ2 prop_sec2  i.year satmat satverb legacy sex* advsex* advrank*  if sampletag==1 & islam_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj1
areg  outcomechrist islam_school christ_school newtreatmusl  newtreatchrist prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if sampletag==1 & islam_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj2
areg  outcomesecular islam_school christ_school newtreatmusl  newtreatchrist  prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if sampletag==1 & islam_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj3
esttab maj* using TableB1.tex, label  nocon keep(newtreatmusl  newtreatchrist) b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) replace
	

*For students from Christian Schools:
areg outcomemusl islam_school christ_school newtreatmusl newtreatchrist prop_islam2 prop_christ2 prop_sec2  i.year satmat satverb legacy sex* advsex* advrank*  if  sampletag==1 & christ_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj1
areg  outcomechrist islam_school christ_school newtreatmusl  newtreatchrist prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if  sampletag==1 & christ_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj2
areg  outcomesecular islam_school christ_school newtreatmusl  newtreatchrist  prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if  sampletag==1 & christ_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj3
esttab maj* using TableB1.tex, label  nocon keep(newtreatmusl  newtreatchrist) b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) append
	

*For students from Secular Schools:
areg outcomemusl islam_school christ_school newtreatmusl newtreatchrist prop_islam2 prop_christ2 prop_sec2  i.year satmat satverb legacy sex* advsex* advrank*  if  sampletag==1 & secular_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj1
areg  outcomechrist islam_school christ_school newtreatmusl  newtreatchrist prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if  sampletag==1 & secular_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj2
areg  outcomesecular islam_school christ_school newtreatmusl  newtreatchrist  prop_islam2 prop_christ2 prop_sec2 i.year satmat satverb legacy sex* advsex* advrank* if  sampletag==1 & secular_school==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj3
esttab maj* using TableB1.tex, label  nocon keep(newtreatmusl  newtreatchrist) b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) append
	
									********************************************************************************************************
									*****************Table B2 in appendix* Robustness Checks for First year course taking behavior********** 
									********************************************************************************************************

									
*a)Conditioning on classes where we can identify more than one section

bys  Term CourseNum Subject  : gen TotalClass2 = _N
*after this condition regressions wh ere TotalClass2!=TotalClass
areg outcomemusl islam_school christ_school newtreatmusl newtreatchrist   i.year satmat satverb legacy sex* advsex* advrank*  if year>=2002 & year<=2017 & bla<=10 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & SemGPA!=. & MAJR_CODE!="MJRL" & qualitychris!=. & qualitymusl!=. & TotalClass!=TotalClass2 ,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj1
areg outcomechrist islam_school christ_school newtreatmusl newtreatchrist   i.year satmat satverb legacy sex* advsex* advrank*  if year>=2002 & year<=2017 & bla<=10 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & SemGPA!=. & MAJR_CODE!="MJRL" & qualitychris!=. & qualitymusl!=. & TotalClass!=TotalClass2 ,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj2
areg outcomesecular islam_school christ_school newtreatmusl newtreatchrist   i.year satmat satverb legacy sex* advsex* advrank*  if year>=2002 & year<=2017 & bla<=10 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & SemGPA!=. & MAJR_CODE!="MJRL" & qualitychris!=. & qualitymusl!=. & TotalClass!=TotalClass2 ,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj3

esttab maj* using TableB2.tex, label  nocon keep(newtreatmusl  newtreatchrist) b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) replace
	
		
*b) 2nd Robustness Check 
qui{
areg outcomemusl islam_school christ_school newtreatmusl newtreatsec prop_islam2 prop_christ2 prop_sec2  i.year satmat satverb legacy sex* advsex* advrank*  if year>=2002 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & SemGPA!=. & MAJR_CODE!="MJRL" & qualitychris!=. & qualitymusl!=. ,  absorb( Dept) vce(cluster advisorIDyear)
drop sampletag
	gen sampletag=1 if e(sample)	
}
areg outcomemusl islam_school christ_school newtreatmusl newtreatchrist   i.year satmat satverb legacy sex* advsex* advrank*  if sampletag==1 ,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj1
areg outcomechrist islam_school christ_school newtreatmusl newtreatchrist   i.year satmat satverb legacy sex* advsex* advrank*  if sampletag==1,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj2
areg outcomesecular islam_school christ_school newtreatmusl newtreatchrist   i.year satmat satverb legacy sex* advsex* advrank*  if sampletag==1 ,  absorb( Dept) vce(  cluster advisorIDyear)
eststo maj3 

esttab maj* using TableB2.tex, label  nocon keep(newtreatmusl  newtreatchrist) b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) append
	
		
